#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "chinesecalendar.h"
#include <QMessageBox>
#include <QDateTime>
#include "MonthInfo.h"
#include "ChnMonthInfo.h"
#include "DayInfo.h"
#include "support.h"
#include "names.h"
#include "stdio.h"
#include "calendar_func.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pBTN_Lunar_clicked()
{
    CChineseCalendar chinesecal;
    WZDAYTIME *pDaytime = new WZDAYTIME;
    int min = ui->spinBox_Min->value();
    int max = ui->spinBox_Max->value();
    int N = max - min;
    for(int i = min;i<max+1;i++)
    {
        if(chinesecal.SetGeriYear(i))
        {
            CChnMonthInfo mon = chinesecal.GetChnMonthInfo(8);
            double jdday = mon.GetNewMoonDayJD()+22;
            ui->dSpinBox_jd->setValue(jdday);

            GetDayTimeFromJulianDay(jdday,pDaytime);
            QDateTime dt;
            dt.setDate(QDate(pDaytime->year, pDaytime->month, pDaytime->day));
            dt.setTime(QTime(pDaytime->hour,pDaytime->minute,pDaytime->second));
            ui->dateTime_gl->setDateTime(dt);
//            if(pDaytime->month == 10 && pDaytime->day == 15)
//            {
//                dt.setDate(QDate(pDaytime->year, pDaytime->month, pDaytime->day));
//                dt.setTime(QTime(pDaytime->hour,pDaytime->minute,pDaytime->second));
//                ui->dateTime_gl->setDateTime(dt);
//            }
            ui->progressBar->setValue(int((i-min)*100/N));
        }

    }
    delete pDaytime;
    //int cyear = chinesecal.GetCurrentYear();
    //CChnMonthInfo mon = chinesecal.GetChnMonthInfo(2);
    //ui->dSpinBox_jd->setValue(mon.GetNewMoonDayJD());
    //ui->lineEdit->setText(QString::number(mon.GetMonthDays()));
    //double nwday = mon.GetNewMoonDayJD();
    //int monday = mon.GetMonthDays();
//    QMessageBox msgBox;
//    msgBox.setText("The document has been modified.");
//    msgBox.setInformativeText(QString::number(nwday));
//    msgBox.setStandardButtons(QMessageBox::Save);
//    msgBox.setDefaultButton(QMessageBox::Save);
//    int ret = msgBox.exec();
    chinesecal.GetCurrentYear();
}

void MainWindow::on_pushButton_2_clicked()
{
    CChineseCalendar chinesecal;
    WZDAYTIME *pDaytime = new WZDAYTIME;
    int min = ui->spinBox_Min->value();

    if(chinesecal.SetGeriYear(min))
    {
//        chinesecal.GetSolarTermsName()

        CChnMonthInfo mon = chinesecal.GetChnMonthInfo(ui->spinBox_Mon->value());
        int name = mon.GetMonthName();
        QString m = "";
        if(mon.IsLeapMonth())
            m += "闰";
        double jdday = mon.GetNewMoonDayJD()+22;
        ui->dSpinBox_jd->setValue(jdday);

        GetDayTimeFromJulianDay(jdday,pDaytime);
        QDateTime dt;
        dt.setDate(QDate(pDaytime->year, pDaytime->month, pDaytime->day));
        dt.setTime(QTime(pDaytime->hour,pDaytime->minute,pDaytime->second));
        ui->dateTime_gl->setDateTime(dt);
        names nams;
        ui->lineEdit->setText(m +nams.nameOfChnMonth.at(name));
    }
}

void MainWindow::on_calendarWidget_clicked(const QDate &date)
{
    CChineseCalendar chinesecal;
    chinesecal.SetGeriYear(date.year());
    CChnMonthInfo mon = chinesecal.GetChnMonthInfo(date.month());
    CMonthInfo mInfo = chinesecal.GetMonthInfo(date.month());
    QString m = "";
    if(mon.IsLeapMonth())
        m += "闰";
    CDayInfo dInfo = mInfo.GetDayInfo(date.day());
    int ChnDate = dInfo.GetDayChnDate();
    int ChnMonth = dInfo.GetDayChnMonth();
    ui->lineEdit->setText(m+ QString::number(ChnMonth)+tr("月") + QString::number(ChnDate) + tr("日"));
//    double jd = CalculateJulianDay(date.year(),date.month(),date.day(),12,0,0);
//    CChnMonthInfo mon = chinesecal.FindChnMonthInfo(jd);
//    CHN_MONTH_INFO *info = new CHN_MONTH_INFO;
//    mon.GetChnmonthInfo(info);
//    names na;
//    QString m = "";
//    if(mon.IsLeapMonth())
//        m += "闰";
//    int *strem =new int;
//    int *branch =new int;
//    CalculateStemsBranches(date.year(), strem, branch);
//    ui->dateTime_gl->setDate(date);
//    //ui->lineEdit->setText(na.nameOfStems.at(*strem)+na.nameOfBranches.at(*branch)+tr("年")+m+na.nameOfChnMonth.at(info->mname-1)+tr("月") + na.nameOfChnDay.at(int(jd) - int(info->shuoJD)-1) + tr("日"));
//    ui->lineEdit->setText(na.nameOfStems.at(*strem)+na.nameOfBranches.at(*branch)+tr("年")+m+na.nameOfChnMonth.at(info->mname-1)+tr("月") + QString::number(int(jd) - int(info->shuoJD)-1) + tr("日"));
}
